package com.wd.live.module_wide.view.fragment

import android.content.Intent
import android.graphics.Typeface
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.widget.AdapterView.OnItemClickListener
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.google.android.material.tabs.TabLayout
import com.jcodecraeer.xrecyclerview.XRecyclerView
import com.wd.live.module_base.net.bean.wide.FindLiveListBean
import com.wd.live.module_base.net.bean.wide.FindLiveTypeListBean
import com.wd.live.module_base.ui.base.BaseFragment
import com.wd.live.module_wide.R
import com.wd.live.module_wide.databinding.FragmentWideBinding
import com.wd.live.module_wide.view.activity.ConstellationActivity
import com.wd.live.module_wide.view.activity.SearchActivity
import com.wd.live.module_wide.view.adapter.FindLiveListAdapter
import com.wd.live.module_wide.viewmodel.WideViewModel
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter

/**
 * A simple [Fragment] subclass.
 * Use the [WideFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
@Route(path = "/wide/widefragment")
class WideFragment : BaseFragment<WideViewModel, FragmentWideBinding>() {
    private var labs = mutableListOf<FindLiveListBean>()
    private var labsType= mutableListOf<FindLiveTypeListBean>()
    private var typeArr= mutableListOf<Int>()
    var liveType:Int=101
    var page:Int=1
    var size:Int=8

    override fun initData() {

        //直播分类请求
        viewModel.getFindLiveTypeList()
        //viewmodel返回数据
        viewModel.dataFindLiveTypeList.observe(this){
            labsType.clear()
            it.result?.let {
                //添加到集合
                    it1->labsType.addAll(it1)
            }
            labsType.forEach{typeBean->
                //tab设置文字切换
                dataBinding.wideTb.addTab(dataBinding.wideTb.newTab().setText(typeBean.name))
                //把id放在集合中
                typeArr.add(typeBean.id)
            }

            //选中切换监听事件
            dataBinding.wideTb.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
                override fun onTabSelected(tab: TabLayout.Tab?) {
                    if (tab != null) {
                        labs.clear()
                        liveType=typeArr[tab.position]
                        viewModel.getFindLiveList(liveType, page, size)
                        setTab(tab)
                    }
                }

                override fun onTabUnselected(tab: TabLayout.Tab?) {
                    tab?.customView=null
                }

                override fun onTabReselected(tab: TabLayout.Tab?) {

                }

            })

            //直播列表
            viewModel.getFindLiveList(liveType, page, size)

            viewModel.dataFindLiveList.observe(this){
                if (page == 1) {
                    labs.clear()
                }
                dataBinding.wideXr.reset()
                it.result?.let {
                        it1 -> labs.addAll(it1)
                }

                dataBinding.wideXr.adapter?.notifyDataSetChanged()
            }
            //适配器
            val findLiveListAdapter = FindLiveListAdapter(context, R.layout.live_list_item, labs)
            dataBinding?.wideXr?.adapter=findLiveListAdapter
            //列表样式
            val gridLayoutManager = GridLayoutManager(context, 2)
            dataBinding?.wideXr?.layoutManager=gridLayoutManager
            //条目点击事件
            findLiveListAdapter.setOnItemClickListener(object : MultiItemTypeAdapter.OnItemClickListener{
                override fun onItemClick(p0: View?, p1: RecyclerView.ViewHolder?, p2: Int) {
                    ARouter.getInstance().build("/wide/watch_activity")
//                        .withSerializable("liveData",labs[p2-1])
                        .withString("nickName",labs[p2-1].nickName)
                        .withString("headPic",labs[p2-1].headPic)
                        .withString("liveTitle",labs[p2-1].liveTitle)
                        .withString("liveCoverUrl",labs[p2-1].liveCoverUrl)
                        .withString("liveTime",labs[p2-1].liveTime)
                        .withString("channelName",labs[p2-1].channelName)
                        .withInt("liveId",labs[p2-1].liveId)
                        .withInt("anchorId",labs[p2-1].anchorId)
                        .withInt("liveStatus",labs[p2-1].liveStatus)
                        .withInt("follow",labs[p2-1].follow)
                        .withInt("sex",labs[p2-1].sex)
                        .navigation()
                }

                override fun onItemLongClick(
                    p0: View?,
                    p1: RecyclerView.ViewHolder?,
                    p2: Int
                ): Boolean {
                    return false
                }
            })
            //上拉刷新下拉加载
            dataBinding.wideXr.setLoadingListener(object :XRecyclerView.LoadingListener{
                override fun onRefresh() {
                    page=1
                    viewModel.getFindLiveList(liveType, page, size)

                }

                override fun onLoadMore() {
                    page++
                    viewModel.getFindLiveList(liveType, page, size)
                }

            })
        }

        //星座匹配
        dataBinding.squarePlayStarSignId.setOnClickListener(View.OnClickListener {
            startActivity(Intent(context,ConstellationActivity::class.java))
        })

        dataBinding.search.setHint("请输入主播昵称")
        //搜索
        dataBinding.wideSearchTop.setOnClickListener(View.OnClickListener {
            startActivity(Intent(context,SearchActivity::class.java))
        })



    }

    //选中切换大小
    fun setTab(tab: TabLayout.Tab?){
        val textView= TextView(activity)
        //字体样式
        val selectedSize =
            TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 20f, resources.displayMetrics)
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, selectedSize)
        textView.typeface = Typeface.defaultFromStyle(Typeface.BOLD) //加粗
        textView.gravity = Gravity.CENTER
        //选中的字体颜色
        //textView.setTextColor(ContextCompat.getColor(context, Color.BLACK))
        textView.text = tab!!.text
        tab.customView = textView

    }
    override fun getViewModelClass(): Class<WideViewModel> {
       return WideViewModel::class.java
    }

    override fun getLayout(): Int {
       return R.layout.fragment_wide
    }


}